package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.LikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import com.mongodb.client.result.DeleteResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {

    //获取行为实体类
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    MongoTemplate mongoTemplate;

    /**
     *  点赞或取消点赞
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(LikesBehaviorDTO dto) {
        //获取到当前用户登录的信息
        ApUser user = AppThreadLocalUtils.getUser();
        if(user == null){
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }

        //根据当前登录用户id查询实体对象 有就查询出来没有就创建
        ApBehaviorEntry apBehaviorEntry= apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if(apBehaviorEntry == null){
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR);
        }
        //创建实体类对象
        ApLikesBehavior apLikesBehavior = new ApLikesBehavior();
        //判断是点赞操作 还是 取消点赞操作
        if(dto.getOperation().equals((short)0)){
            //为0 的话就是点赞
            //去查询mongo数据库 如果 有某条数据 的entryId 的值为 当前用户id或设备id 且 articleId 为当前文章id 的话 那么 就属于已经点赞过了
            Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
            ApLikesBehavior likesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
            if(likesBehavior != null){
                CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"已经点赞过 不可重复点赞");
            }
            //如果查询的为空的话 就是没有点赞过 允许操作 并sava记录
            apLikesBehavior.setCreatedTime(new Date());
            apLikesBehavior.setEntryId(apBehaviorEntry.getId());
            apLikesBehavior.setArticleId(dto.getArticleId());
            apLikesBehavior.setType(dto.getType());
            ApLikesBehavior save = mongoTemplate.save(apLikesBehavior);
            log.info("点赞操作 以创建行为 返回结果为:{}",save);
        }else if(dto.getOperation().equals((short)1)){
            //为1的话就是取消点赞 删除对应关系
            Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
            ApLikesBehavior likesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
            if(likesBehavior == null){
                CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
            }
            //删除 对应的数据
             mongoTemplate.remove(query, ApLikesBehavior.class);
        }
        return ResponseResult.okResult();
    }
}
